import 'package:flutter/material.dart';
import 'package:learning/constants/app_images.dart';
import 'package:learning/constants/app_strings.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';

class WelcomePage extends StatelessWidget {
  WelcomePage({super.key, required this.skipAction});
  final _pageController = PageController(initialPage: 0);
  final Function skipAction;

  /// 构建单页面
  Widget _buildSinglePage(
      {required BuildContext context,
      required String image,
      required String title,
      required String description}) {
    return Column(
      children: [
        Image.asset(
          image,
          fit: BoxFit.fitWidth,
        ),
        Container(
          margin: const EdgeInsets.only(top: 15),
          child: Text(
            title,
            textAlign: TextAlign.center,
            style: Theme.of(context).textTheme.headlineSmall,
          ),
        ),
        Container(
          margin: const EdgeInsets.only(top: 15),
          padding: const EdgeInsets.symmetric(horizontal: 30),
          child: Text(
            description,
            textAlign: TextAlign.center,
            style: Theme.of(context).textTheme.bodyLarge,
          ),
        ),
      ],
    );
  }

  /// 构建页面视图
  Widget _buildPageView(BuildContext context) {
    return PageView(
      controller: _pageController,
      scrollDirection: Axis.horizontal,
      reverse: false, // 用于控制页面滚动的方向。
      pageSnapping: true, // 用于控制页面滚动时是否启用页面捕捉效果。
      physics: const ClampingScrollPhysics(), // 用于控制页面滚动时的物理效果。
      children: [
        _buildSinglePage(
          context: context,
          image: AppImages.welcome1,
          title: AppStrings.welcomeTitle1,
          description: AppStrings.welcomeDesc1,
        ),
        _buildSinglePage(
          context: context,
          image: AppImages.welcome2,
          title: AppStrings.welcomeTitle2,
          description: AppStrings.welcomeDesc2,
        ),
        _buildSinglePage(
          context: context,
          image: AppImages.welcome3,
          title: AppStrings.welcomeTitle3,
          description: AppStrings.welcomeDesc3,
        ),
      ],
    );
  }

  /// 构建指示器
  Widget _buildIndicator(BuildContext context) {
    return SmoothPageIndicator(
      controller: _pageController,
      count: 3,
      effect: WormEffect(activeDotColor: Theme.of(context).colorScheme.primary),
    );
  }

  Widget _buildSkipButton(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        MaterialButton(
          onPressed: () {
            skipAction();
          },
          child: Text(
            AppStrings.skip,
            style: Theme.of(context).textTheme.headlineSmall,
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Theme.of(context).scaffoldBackgroundColor,
      child: SafeArea(
        child: Column(
          children: [
            Expanded(flex: 3, child: _buildPageView(context)),
            Expanded(
                flex: 1,
                child: Column(
                  children: [
                    const SizedBox(height: 20),
                    _buildIndicator(context),
                    const Spacer(),
                    _buildSkipButton(context),
                    const SizedBox(height: 40),
                  ],
                )),
          ],
        ),
      ),
    );
  }
}
